package com.pudu.pudusclient;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.carl.recyclerview.SnappingSwipingViewBuilder;
import com.carl.recyclerview.SnappyLinearLayoutManager;
import com.carl.recyclerview.SwipeGestureHelper;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

import java.util.ArrayList;
import java.util.List;

public class CustomViewActivity extends AppCompatActivity implements SwipeGestureHelper.OnSwipeListener{

    private SimpleAdapter mAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!hasActionBar()) {
            supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                    WindowManager.LayoutParams. FLAG_FULLSCREEN);
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }

        setContentView(R.layout.custom_root_layout);

        RelativeLayout rl = (RelativeLayout) findViewById(R.id.container);

        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        mAdapter = new SimpleAdapter(strings);

        RecyclerView recyclerView = new SnappingSwipingViewBuilder(this)
                .setAdapter(mAdapter)
                .setHeadTailExtraMarginDp(0)
                .setItemMarginDp(0, 0, 0, 0)
                .setOnSwipeListener(this)
                .setSnapMethod(SnappyLinearLayoutManager.SnappyLinearSmoothScroller.SNAP_CENTER)
                .build();

        if (rl != null) {
            recyclerView.setLayoutParams(new ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            rl.addView(recyclerView);
        }
    }

    protected boolean hasActionBar() {
        return  false;
    }

    @Override
    public void onSwipe(RecyclerView rv, int adapterPosition, float dy) {

    }


    class SimpleViewHolder extends RecyclerView.ViewHolder {

        public final ImageView mimgView;

        public SimpleViewHolder(View ll, ImageView itemView) {
            super(ll);
            mimgView = itemView;
        }
    }

    class SimpleAdapter extends RecyclerView.Adapter<SimpleViewHolder> {
        private final List<String> mDataSet;

        public SimpleAdapter(List<String> dataSet) {
            this.mDataSet = dataSet;
        }

        public void removeItem(int adapterPos) {
            mDataSet.remove(adapterPos);
            notifyItemRemoved(adapterPos);
        }

        @Override
        public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Log.d(App.MYTAG, "onCreateViewHolder");

/*            TextView tv = new TextView(parent.getContext());
            tv.setTextColor(Color.WHITE);

            int w = parent.getWidth();
            int itemMargin = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 9F,
                    getResources().getDisplayMetrics()) + 0.5F);
            int itemPadding = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8F,
                    getResources().getDisplayMetrics()) + 0.5F);
            int itemWidth = w - (itemMargin + itemPadding * 2) * 2;
            tv.setLayoutParams(new ViewGroup.LayoutParams(
                    itemWidth, ViewGroup.LayoutParams.MATCH_PARENT));*/

            ImageView imgview = new ImageView(parent.getContext());
            int itemWidth = parent.getWidth();

            imgview.setLayoutParams(new ViewGroup.LayoutParams(
                    itemWidth, ViewGroup.LayoutParams.MATCH_PARENT));



            return new SimpleViewHolder(imgview, imgview);
        }

        private int getColor(int position) {
            float factor = (float) position / (float) getItemCount();
            return 0xFF000000 | (int) (0x0000FF * Math.sin(factor))
                    | ((int) (0x0000FF * Math.sin(2 * Math.PI * (factor + 1F / 3F))) << 8)
                    | ((int) (0x0000FF * Math.sin(2 * Math.PI * (factor + 2F / 3F))) << 16);
        }

        @Override
        public void onBindViewHolder(SimpleViewHolder holder, int position) {
            Log.d(App.MYTAG, "onBindViewHolder: " + holder.itemView);
            ImageView imgView = holder.mimgView;

            //TextView tv = holder.mTextView;
            //String content = mDataSet.get(position);
            //tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
            //tv.setGravity(Gravity.CENTER);
            //tv.setText(content);
            //int color = getColor(position);
            //Log.d(App.MYTAG, String.format("color: %8h", color));
            //tv.setBackgroundColor(color);


        }

        @Override
        public int getItemCount() {
            return mDataSet.size();
        }
    }







    private Handler mHandler01 = new Handler();

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        try {
            mHandler01.post(mTask01);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        try {
            mHandler01.post(mTask01);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return super.dispatchTouchEvent(ev);
    }

    private Runnable mTask01 = new Runnable() {

        @Override
        public void run() {
            try {
                CustomViewActivity.this.finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}
